package cz.schovjan.mte_proj.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import cz.schovjan.mte_proj.R;

public class SearchListFragment extends Fragment {

    private ListView lvItems;
    private SearchTypeCallBack callback;// where to inform about selection

    public interface SearchTypeCallBack {

	public void onSearchTypeChoose(int type);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_search_list, container);
	lvItems = (ListView) v.findViewById(R.id.item_list);
	lvItems.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {
		// inform activity about selection
		callback.onSearchTypeChoose(position);
	    }
	});
	return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
	super.onActivityCreated(savedInstanceState);
	// create some fake data
	ListAdapter adapter = new ArrayAdapter<String>(
		getActivity(),
		android.R.layout.simple_list_item_1,
		new String[] { getResources().getString(R.string.by_artist_song), getResources().getString(R.string.by_text) });
	lvItems.setAdapter(adapter);
    }


    @Override
    public void onAttach(Activity activity) {
	super.onAttach(activity);
	if (!(activity instanceof SearchTypeCallBack)) {
	    throw new ClassCastException("Attaching activity must implement interface");
	}
	callback = (SearchTypeCallBack) activity;
    }
}
